package org.manager.units;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.manager.generic.web.JsonViewUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/classHours")
public class ClassHoursController {
	
	@Resource(name="classHoursRepository")
	ClassHoursRepository  classHoursRepository;
	
	private static final String classHoursAllField[] = new String[]{"id","unitId","unitCode","code","name","shortname","positionDes","type","status","order","agenciesType"};

	/**
	 * 分页查询数据
	 * @param code
	 * @param start
	 * @param limit
	 * @return
	 */
	@RequestMapping(params="method=page")
	public ModelAndView page(HttpServletRequest request,
			HttpServletResponse response,String code, String start, String limit) {
		String id = request.getParameter("propertyId");
		String searchCode = request.getParameter("searchCode");
		String searchName = request.getParameter("searchName");
		if(id!=null &&!id.equals("")){
			request.getSession().setAttribute("unitid", id);
		}
		int istart = 0;
		int ilimit = 10;
		if(NumberUtils.isNumber(start)){
			istart = NumberUtils.toInt(start);
		}
		
		if(NumberUtils.isNumber(limit) && NumberUtils.toInt(limit)>0){
			ilimit = NumberUtils.toInt(limit);
		}
		ClassHours sysUnit = new ClassHours();
		sysUnit.setUnitId((String)request.getSession().getAttribute("unitid"));
		if(searchCode!=null && !searchCode.equals("")){
			sysUnit.setCode(StringUtils.trimToEmpty(searchCode));
		}
		if(searchName!=null && !searchName.equals("")){
			sysUnit.setName(StringUtils.trimToEmpty(searchName));
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("totalProperty", classHoursRepository.totalRecord(sysUnit));
		map.put("data", classHoursRepository.findPageClassHours(sysUnit, istart, ilimit));
		map.put("start", istart);
		map.put("limit", ilimit);

		return JsonViewUtil.getSuccessViewWithIncludProperty(map, classHoursAllField);
	}
	
	/**
	 * 添加科室
	 * @param request
	 * @param response
	 * @param classHours
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(params="method=add",method=RequestMethod.POST)
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response,ClassHours classHours) throws IOException{
		Serializable id = null;
		String result = "";
		id = classHoursRepository.save(classHours);
		if(id!=null){
			result = "{message:'插入成功!',success:true}";
		}
		return JsonViewUtil.getSuccessViewWithStringDataArray(result);
	}
	
	/**
	 * 修改科室
	 * @param request
	 * @param response
	 * @param classHours
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(params="method=update",method=RequestMethod.POST)
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response,ClassHours classHours) throws IOException{
		String result = "";
		classHoursRepository.update(classHours);
		result = "{message:'修改成功!',success:true}";
		return JsonViewUtil.getSuccessViewWithStringDataArray(result);
	}
	
	/**
	 * 修改时绑定科室
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(params = "method=findByCode")
	public ModelAndView findByCode(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setContentType("text");
		response.setCharacterEncoding("UTF-8");
		ClassHours content = classHoursRepository.get(request.getParameter("id"));
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("success : true,");
		sb.append("data : {id :\"" + content.getId() + "\",code : \""+content.getCode()+"\",name : \"" +
				""+content.getName()+"\",unitId : \""+content.getUnitId()+"\",unitCode : \"" +
						""+content.getUnitCode()+"\",shortname : \""+content.getShortname()+"\"" +
								",positionDes : \""+content.getPositionDes()+"\",type : \""+content.getType()+"\"" +
										",status : \""+content.getStatus()+"\",order : \""+content.getOrder()+"\",agenciesType : \""+content.getAgenciesType()+"\"}");
		sb.append("}");
		return JsonViewUtil.getSuccessViewWithStringDataArray(sb.toString());
	}
	
	/**
	 * 添加是绑定科室编号
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(params = "method=saveFindByCode")
	public ModelAndView saveFindByCode(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setContentType("text");
		response.setCharacterEncoding("UTF-8");
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("success : true,");
		sb.append("data : {unitId : \""+request.getParameter("id")+"\"}");
		sb.append("}");
		return JsonViewUtil.getSuccessViewWithStringDataArray(sb.toString());
	}
	
	/**
	 * 删除科室
	 * @param ids
	 * @return
	 */
	@RequestMapping(params="method=delete")
	public ModelAndView del(String ids){
		String id[] = ids.split(",");
		if(StringUtils.isNotBlank(ids)){
			for(String s : id){
				classHoursRepository.deleteByKey(s);
			}
			return JsonViewUtil.getSuccessView();
		}
		return JsonViewUtil.getFailureView();
	}
	
}
